package com.stylefeng.guns.modular.bus.service.impl;

import com.stylefeng.guns.core.util.Config;
import com.stylefeng.guns.core.util.FileUtil;
import com.stylefeng.guns.core.util.KaptchaUtil;
import com.stylefeng.guns.core.util.OSSUtil;
import com.stylefeng.guns.modular.bus.service.IBusPrductInfoService;
import com.stylefeng.guns.modular.system.model.BusPrdTrace;
import com.stylefeng.guns.modular.system.dao.BusPrdTraceMapper;
import com.stylefeng.guns.modular.bus.service.IBusPrdTraceService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.system.model.BusPrductInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author junz123
 * @since 2018-04-21
 */
@Service
public class BusPrdTraceServiceImpl extends ServiceImpl<BusPrdTraceMapper, BusPrdTrace> implements IBusPrdTraceService {
    @Resource
    BusPrdTraceMapper busPrdTraceMapper;
    @Resource
    private IBusPrductInfoService busPrductInfoService;
    public String createBatchQrcodeFile(BusPrdTrace busPrdTrace)
    {
        int prdTraceMinimum=Integer.parseInt(busPrdTrace.getPrdTraceMinimum());
        int batchCount=busPrdTrace.getPrdTraceBatchcount();
        String datadate=busPrdTrace.getDatadate()+"";
        String areaCode=busPrdTrace.getPrdSellArea();
        BusPrductInfo busPrductInfo=busPrductInfoService.selectById(busPrdTrace.getPrdId());
        String prdNo=busPrductInfo.getPrdNo();
        StringBuffer content=new StringBuffer();
        String batchId=busPrdTrace.getId();
        for (int i = prdTraceMinimum; i <=prdTraceMinimum+batchCount; i++) {
            content.append(KaptchaUtil.encryptPrdTraceUrl(i,areaCode,prdNo,datadate)+"\r\n");
        }
        //String fileName=new Date().getTime()+".txt";
        String fileName=batchId+".txt";

        fileName=OSSUtil.putSingleFile(content.toString().getBytes(),fileName);
        /*String fileName=this.getClass().getResource("/").getPath()+ File.separator+
        FileUtil.writeFileContent()*/
        busPrdTrace.setQrCodeFileName(fileName);
        return  fileName;
    }

    @Override
    public Integer insertTrace(BusPrdTrace busPrdTrace) {
        return busPrdTraceMapper.insertTrace(busPrdTrace);
    }

    @Override
    public List<BusPrdTrace> selectTraceBatchByMap(Map map) {
        return busPrdTraceMapper.selectTraceBatchByMap(map);
    }
}
